package abcy.cloud.configserver.services.impl;

import abcy.cloud.blogcommon.utils.UUIDUtil;
import abcy.cloud.configserver.entities.Config;
import abcy.cloud.configserver.http.ConfigAddReq;
import abcy.cloud.configserver.mappers.ConfigMapper;
import abcy.cloud.configserver.services.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
@Slf4j
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigMapper configMapper;

    @Autowired
    RestTemplate restTemplate;


    @Override
    public boolean addConfig(ConfigAddReq configAddReq) {
        log.info("新增配置参数：{}", configAddReq);
        String id = UUIDUtil.UUID().toString().replace("-", "");
        Config config = new Config();
        BeanUtils.copyProperties(configAddReq, config);
        config.setId(id);
        boolean flag = false;
        int value = configMapper.insert(config);
        if(value == 1) {
           flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    @Override
    public void test() {
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String urlStr = "http://localhost:" + 8888 + "/sys/sysuser/getUserInfoByUserName";
        /*Map<?,?> resultMap1 = (Map<?, ?>) restTemplate.exchange(urlStr, HttpMethod.POST,
                new HttpEntity<MultiValueMap<String, String>>(formData, headers), Map.class);*/
        restTemplate.exchange(urlStr, HttpMethod.POST,
                new HttpEntity<MultiValueMap<String, String>>(formData, headers), Map.class).getBody();
    }
}
